package topcbl.taxi;

import topcbl.net.TaxiService;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.Spanned;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class RegisterActivity extends Activity {
	Button btn_register;
	EditText edt_username, edt_password, edt_confirm;
	TaxiService mService;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register);
		getViewById();
		mService = MainActivity.mBindService.getService();
		btn_register.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (checkEditText()) {
					String acc = edt_username.getText() + "";
					String pass = edt_password.getText() + "";
					if (mService.register(acc, pass)) {
						Toast.makeText(RegisterActivity.this,
								getString(R.string.register_waiting_for_login),
								Toast.LENGTH_LONG).show();
						// login if register successful
						if (mService.login(acc, pass)) {
							Intent login = new Intent(RegisterActivity.this,
									MainActivity.class);
							startActivity(login);
						} else {
							Toast.makeText(
									RegisterActivity.this,
									getResources()
											.getString(
													R.string.splash_text_wrong_acc_pass),
									Toast.LENGTH_SHORT).show();
						}
					} else {
						Toast.makeText(RegisterActivity.this,
								getString(R.string.register_register_failed),
								Toast.LENGTH_LONG).show();
					}
				}
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.register, menu);
		return true;
	}

	void getViewById() {
		btn_register = (Button) findViewById(R.id.btnRegister);
		edt_username = (EditText) findViewById(R.id.edtUsername);
		edt_password = (EditText) findViewById(R.id.edtPassword);
		edt_confirm = (EditText) findViewById(R.id.edtConfirmPassword);

		edt_username.setFilters(new InputFilter[] { new InputFilter() {
			public CharSequence filter(CharSequence src, int start, int end,
					Spanned dst, int dstart, int dend) {
				if (src.equals("")) { // for backspace
					return src;
				}
				if (src.toString().matches("[a-zA-Z0-9 ]+")) {
					return src;
				}
				return "";
			}
		} });
	}

	boolean checkEditText() {
		if (edt_username.getText().length() <= 0) {
			Toast.makeText(RegisterActivity.this,
					getString(R.string.register_username_require),
					Toast.LENGTH_LONG).show();
			return false;
		}
		if (edt_password.getText().length() <= 0) {
			Toast.makeText(RegisterActivity.this,
					getString(R.string.register_password_require),
					Toast.LENGTH_LONG).show();
			return false;
		}
		if (edt_confirm.getText().length() <= 0) {
			Toast.makeText(RegisterActivity.this,
					getString(R.string.register_confirm_password_require),
					Toast.LENGTH_LONG).show();
			return false;
		}
		if (!edt_confirm.getText().toString()
				.equals(edt_password.getText().toString())) {
			Toast.makeText(RegisterActivity.this,
					getString(R.string.register_confirm_password_match),
					Toast.LENGTH_LONG).show();
			return false;
		}
		return true;
	}
}
